Skip to content

fix: surface repeat acp_retrieve cost in the ack (#1430) - #1674

Closed
ranxianglei wants to merge 1 commit into
masterfrom
2026-09-29_ccr-retrieve-cost-hint
Closed

ranxianglei wants to merge 1 commit into
masterfrom
2026-09-29_ccr-retrieve-cost-hint

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Fixes #1430

What & why

#1430: repeated acp_retrieve of the same stored ref re-sends the full original every turn with no feedback at the decision point. Because CCR stores oversized tool results at arrival and the retrieved text is ephemeral (never enters history, never compressible), a ref the model keeps needing gets pulled back in full on every turn. The owner's field scan found single refs fetched up to x8 (~50+ calls) and one file re-pulled full 9x along a session lineage (~70k tok of pure duplication). This drives the amplification loop: retrieve bytes count toward growth -> faster floor hit -> earlier fold -> more reliance on retrieve ("the more often we compress, the more aggressively we retrieve").

This is the B2 fix from the issue's ranked plan - host-only, zero structural risk. It makes the accumulating cost visible at the decision point so the model can self-correct.

Change

src/store.ts::executeRetrieve appends a cumulative-cost note to the retrieve ack from the 2nd fetch of the same ref onward, reusing the existing per-ref count map (recordRetrieveHit now returns the running count). No new state, no type change, no config change.

Example ack on the 3rd fetch of m00585:

retrieved m00585: 7,858 tok (31,425 chars)
[acp_retrieve: m00585 fetched 3x this session - each call re-sends the full original (~7,858 tok); re-fetch only if you need detail you do not already have.]

Behavior change (disclosure)

Repeated-ref acp_retrieve acks gain a trailing cost-note line starting at the 2nd fetch onward. First/single fetches are byte-identical to before; sessions that never re-fetch see no change. Wire-safe in both compression modes: the note rides inside the existing tool-result field (proxy mode strips the ephemeral retrieve result; plugin mode carries it as a short, accurate MCP result).

Config surface

Unchanged - no field added, renamed, or re-semantics'd.

Not included (deferred to owner decision, per the issue's ranking)

Pre-flight

  • npm run typecheck - clean
  • npm test - 3209 pass / 0 fail / 12 skipped (new regression test "acp_retrieve被滥用 导致上下文暴增 #1430: repeat acp_retrieve surfaces a cumulative-cost note in the ack; first fetch stays clean")
  • npm run build - success

Repeated fetches of the same CCR ref re-send the full original every turn with no feedback at the decision point - the root of the #1430 context-bloat loop (the more often we compress, the more aggressively we retrieve).

Append a cumulative-cost note to the retrieve ack from the 2nd fetch onward, reusing the existing per-ref count map (no new state, no type change). First fetches stay byte-identical; single-fetch sessions see no change.

Host-only, zero structural risk: rides inside the existing tool-result field on every wire, correct in both compression modes.
@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-29_ccr-retrieve-cost-hint (6c95332)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.1621-pr.1674.4c51ff2

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr1674.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 PR 已核对完毕:代码正确、CI 全绿、rebase 到最新 master 后复验通过,可以合并(wire 可见变更,按 §7.4 走人工合并)。

① 方向关(审原 issue)

#1430 里你拍的归因是「取回通道无成本闸门」,修复排序 B2(host-only、零 wire 结构变化)= 在决策点让累计成本可见。本 PR 精确实现 B2:executeRetrieve 从同一 ref 第 2 次取回起在 ack 尾部追加成本提示,复用既有 per-ref 计数表。没有越界去做 B1/F1/A(已在 PR 正文列明留给你拍板)。方向无漂移。✔

② 回归审查(既有行为逐项清点)

触碰路径的既有行为 结论
首次/单次取回的 ack 保持——count=1 < 阈值 2,返回原始 result.ackText,逐字节不变;回归测试钉住了(!first.includes("this session"))
重复取回的 ack 有意变更(已披露:第 2 次起多一行提示)——本 PR 目的本身
miss 路径(hallucinated ref) 保持——不计数、不追加,直接返回 kernel 的自纠文案
recordRetrieveHit 返回值 void→number 保持语义——唯一生产调用方是 executeRetrieve(已更新);另一调用方 tests/search-plan-aware.test.ts 忽略返回值,其断言(map ≤512、FIFO 淘汰后计数归零)全部照旧通过
session.retrieveCountsByRef 计数/512 FIFO cap 保持——只加返回值,不动增量与淘汰逻辑;decompress-shared.ts:491 的 plan-aware steering 读同一张表,不受影响
两种压缩模式 wire-safe——所有 lane(proxy 流式 src/loop/core.ts:259-260、非流式 src/stream.ts:57-63、plugin lane 同经 core.ts 统一裁决;mcp.ts 不暴露 retrieve;plugin.ts:702 只是 CCR 关闭时的守卫)汇聚到 executeRetrieve 单点,提示搭在既有 tool-result 字段内,不改协议形状

高危模式排查:无 fail-fast 变吞错、无默认值/阈值漂移(新常量 REPEAT_RETRIEVE_ACK_THRESHOLD=2 就是功能本身且已披露)、无缓存/重试策略变化。

③ 代码审查

  • 提示文案用 retrieveToolName(session)(store.ts:57)而非硬编码常量——尊重用户 ccr.toolName 覆盖,与占位符里模型看到的名字一致。✔
  • 注释写清了为什么从第 2 次触发(跨轮重拉环路第一次对模型可见的点)。✔
  • 无新状态、无类型破坏、无配置面变化(PR 正文「Config surface: Unchanged」属实)。✔

④ 测试有效性

新测试有牙:首次干净(抓阈值误降为 1)、第二次 fetched 2x(抓提示丢失)、第三次 fetched 3x(抓计数不递增)、异 ref 隔离(抓全局计数器污染)。bug 回归时每一条都抓得住。✔

⑤ diff 整洁度

2 文件 +42/-3,每一行都与 PR 目的相关;无关改动、空白重排、锁文件 churn 均无。✔

验证数据

非阻塞观察(4 条,都不拦合并)

  1. 计数是内存态 + 512 FIFO cap(沿用 Feature: CCR v3 — planning-aware retrieval (rank candidates against todo/task state, LineRetriever-style) #1336 既有设计):proxy 重启或单会话 >512 个不同 ref 后,老 ref 的计数会归零重计,提示可能少报次数。纯观感问题,与既有设计一致,接受。
  2. 「each call re-sends the full original」措辞:跨轮重复(即 acp_retrieve被滥用 导致上下文暴增 #1430 实测的病理场景,x8/×9)完全准确;同轮内对同 ref 连调两次时,queueRetrieval 会按 ref 去重、只注入一次,该措辞在这个窄边界上略过强。主导场景无误,建议保留现文案(改它反而增加 prompt 字符串维护面),你定。
  3. PR 正文示例里的 ~7,858 tok 带千分位,实际输出是裸数字 ~7858 tok(${result.entry.tokens} 直出)。host 侧同类提示(renderRetrievalNotes 等)也都是裸数字,代码与惯例一致,只是 PR 描述示例多打了逗号——文档层面的小出入,不影响行为。
  4. 顺带发现(非本 PR 引入):src/server.ts:68 导入了 executeRetrieve 但全文件未使用(unified loop 重构后的残留死导入,typecheck 不报因为没开 noUnusedLocals)。server.ts 是当前热文件(fix: position-preserve mid-history system/developer items on the plugin responses lane (#1638) #1671 刚合入改动),不在本 PR 里动它以免扩大冲突面;要不要单独清理由你定。

一句话中文摘要:本 PR 给重复 acp_retrieve 的 ack 加了从第 2 次起的累计成本提示(决策点可见性,B2 方案),首次取回逐字节不变、两模式 wire-safe、测试有牙、CI 与本地 rebase 复验全绿,可以人工合并。

@ranxianglei

Copy link
Copy Markdown
Owner Author

#1674(重复取回成本提示 #1430)——想法好但建立在 v2 之前的旧 executeRetrieve 上(引用已删除的 queueRetrieval/ackText),合并必炸;要留我按 v2 移植

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

acp_retrieve被滥用 导致上下文暴增

1 participant